<template>
  <div class="login">
    <div class="login-bg">
      <div class="center-box">
        <div class="left"></div>
        <div class="right">
          <h3>欢迎登录</h3>
          <el-form :model="loginForm" :rules="loginFormRules" ref="loginForm" class="loginForm">
            <el-form-item label="" prop="username">
              <el-input v-model="loginForm.username" placeholder="请输入您的用户名"></el-input>
            </el-form-item>
            <el-form-item label="" prop="password">
              <el-input v-model="loginForm.password" type="password" placeholder="请输入您的密码"></el-input>
            </el-form-item>
          </el-form>
          <div class="checkbox">
            <el-checkbox v-model="checked">记住密码</el-checkbox>
          </div>
          <el-button type="primary" class="loginBtn" @click="handelLogin">登录</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        loginForm: {
          username: "",
          password: ""
        },
        checked: false,
        loginFormRules: {
          username: [{
            required: true,
            message: '请输入用户名',
            trigger: 'blur'
          }],
          password: [{
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          }],
        }
      }
    },
    created() {
      if (localStorage.getItem("checked")) {
        this.checked = true
        this.loginForm.username = localStorage.getItem("username")
        this.loginForm.password = localStorage.getItem("password")
      }
    },
    mounted() {
      document.onkeydown = (e) => { // 回车提交表单
        // 兼容FF和IE和Opera
        var theEvent = window.event || e;
        var code = theEvent.keyCode || theEvent.which || theEvent.charCode;
        if (code == 13) {
          this.handelLogin()
        }
      }
    },
    methods: {
      handelLogin() {
        this.$refs.loginForm.validate((valid) => {
          if (valid) {
            this.getData()
          }
        });
      },
      async getData() {
        const {
          data: res
        } = await this.$http({
          url: "user/login?usercode=" + this.loginForm.username + "&password=" + this.loginForm.password,
          method: 'get',
        })
        if (res.code == "0") {
          sessionStorage.setItem("id", res.result.id)
          this.$router.push("/home")
          if (this.checked) {
            localStorage.setItem("checked", true)
            localStorage.setItem("username", this.loginForm.username)
            localStorage.setItem("password", this.loginForm.password)
          } else {
            localStorage.removeItem("username")
            localStorage.removeItem("username")
            localStorage.removeItem("checked")
          }
        } else {
          this.$message.error('用户名或密码错误！')
        }
      },
    }
  }

</script>

<style scoped lang="less">
  .login-bg {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100vw;
    height: 100vh;
    background: linear-gradient(4deg, #00AEFF, #006DA7);

    .center-box {
      width: 1200px;
      height: 608px;
      box-shadow: 0px 6px 114px 0px rgba(0, 109, 167, 0.78);
      display: flex;
      position: relative;
      z-index: 2;

      .left {
        position: relative;
        width: 50%;
        height: 100%;
        background: linear-gradient(4deg, #10BEFF, #1F80F7);
      }

      .left::before {
        content: "后台管理";
        position: absolute;
        top: 40px;
        left: 50%;
        transform: translate(-50%);
        color: #fff;
        font-size: 32px;
        font-weight: 700;
        letter-spacing: 20px;
      }

      .left::after {
        content: "";
        position: absolute;
        bottom: 60px;
        left: 50%;
        transform: translateX(-50%);
        width: 490px;
        height: 66%;
        background: url('../assets/images/login-bg.png') no-repeat center;
        background-size: contain;
      }

      .right {
        flex: 1;
        background: #fff;
        padding: 100px 110px;
        box-sizing: border-box;

        h3 {
          color: #444;
          font-size: 30px;
          text-align: center;
          font-weight: 400;
          margin-bottom: 88px;
        }

        .checkbox {
          display: flex;
          justify-content: flex-end;
        }

        .loginBtn {
          margin-top: 70px;
          width: 100%;
          height: 50px;
        }
      }
    }
  }

  .login-bg::after {
    content: "";
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 323px;
    background: url('../assets/images/login-bottom.png') no-repeat center;
    background-size: cover;
    z-index: 1;
  }

</style>
